package com.woniu.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {
    
    @Autowired
    private  StringRedisTemplate stringRedisTemplate;
    
    @Autowired
    private RedisTemplate<String, Object> redisMapTemplate;

    //新增字符串数据(无过期时间)
    public void set(String key, String value) throws Exception {
        stringRedisTemplate.opsForValue().set(key, value);
    }
    
    //新增字符串数据(有过期时间)
    public void set(String key, String value, long seconds) throws Exception {
        System.out.println(stringRedisTemplate);
        stringRedisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
    }
    
    //删除key
    public void del(List<String> keys) throws Exception {
        stringRedisTemplate.delete(keys);
    }
    
    //查询字符串数据
    public  String get(String key) throws Exception {
        return stringRedisTemplate.opsForValue().get(key);
    }

    
    //判断key是否存在
    public boolean hasKey(String key) throws Exception {
        return Boolean.TRUE.equals(stringRedisTemplate.hasKey(key));
    }
    
    //key自增1
    public void incr(String key) throws Exception {
        stringRedisTemplate.opsForValue().increment(key);
    }
    
    //向ZSET中新增一条数据
    public void zadd(String key, String value, double score) throws Exception {
        stringRedisTemplate.opsForZSet().add(key, value, score);
    }
    
    //使ZSET中某数据的排序值自增
    public void zincrby(String key, String value, double score) throws Exception {
        stringRedisTemplate.opsForZSet().incrementScore(key, value, score);
    }
    
    //查询一个值在ZSET的排序值
    public Double zscore(String key, String value) throws Exception {
        return stringRedisTemplate.opsForZSet().score(key, value);
    }
    
    //删除ZSET中的一个数据
    public void zrem(String key, String value) throws Exception {
        stringRedisTemplate.opsForZSet().remove(key, value);
    }
    
    //查询ZSET中的数据个数
    public Long zsize(String key) throws Exception {
        return stringRedisTemplate.opsForZSet().size(key);
    }
    
    //根据排序值的顺序倒序查某一段数据
    public Set<String> revrange(String key, Integer pageNum, Integer pageSize) throws Exception {
        return stringRedisTemplate.opsForZSet().reverseRange(key, (long) (pageNum - 1) * pageSize, (long) pageSize * pageNum - 1);
    }
    
    //保存一个对象(不过期)
    public void saveObject(String key, Object object) throws Exception {
        redisMapTemplate.opsForValue().set(key, object);
    }
    
    //保存一个对象
    public void saveObject(String key, Object object, long seconds) throws Exception {
        redisMapTemplate.opsForValue().set(key, object, seconds, TimeUnit.SECONDS);
    }
    
    //查询出一个对象
    public Object getObject(String key) throws Exception {
        return redisMapTemplate.opsForValue().get(key);
    }
    
    //新增一个hash数据
    public void hset(String key, String hashKey, Object value) throws Exception {
        stringRedisTemplate.opsForHash().put(key, hashKey, value);
    }


    /**
     * 修改哈希数据中某个字段的值
     */
    public void hsset(String key, String hashKey, Object value) {
        stringRedisTemplate.opsForHash().put(key, hashKey, value.toString());
    }
    
    //新增整个hash数据
    public void hmset(String key, Map<Object, Object> map) throws Exception {
        stringRedisTemplate.opsForHash().putAll(key, map);
    }
    
    //使Hash中的一个数据自增
    public void hincrby(String key, String hashKey, double number) throws Exception {
        stringRedisTemplate.opsForHash().increment(key, hashKey, number);
    }


    /*
    使Hash中的一个数据自增
   */
    public void hincrbyy(String key, Integer num) {
        stringRedisTemplate.opsForHash().increment(key, "cartNum", num);
    }
    
    //取出Hash中的一个属性值
    public Object hget(String key, String hashKey) throws Exception {
        return stringRedisTemplate.opsForHash().get(key, hashKey);
    }
    
    //取出一个Hash中的所有数据
    public Map<Object, Object> hgetall(String key) throws Exception {
        return stringRedisTemplate.opsForHash().entries(key);
    }


    public void zdel(String key)throws Exception {
        stringRedisTemplate.opsForZSet().remove(key);
    }

    /**
     * 从 zset 中删除一个数据
     */
    public long zsetRemove(String key, String value) {
        return stringRedisTemplate.opsForZSet().remove(key, value);
    }


    /**
     * 删除数据
     */
    public void delete(final String key) {
        stringRedisTemplate.delete(key);
    }



    /*
 此命令会覆盖哈希表中已存在的字段。如果哈希表不存在，会创建一个空哈希表，并执行 HMSET 操作
 */
    public void hmmset(String key, Map<String, Object> map) {
        stringRedisTemplate.opsForHash().putAll(key, map);
    }

    public boolean exist(String key) {
        return stringRedisTemplate.hasKey(key);
    }
}
